﻿/**
*tree view define
*/
var DefaultBlankIco = "./stylesheets/tree/blank.png";
var DefaultElementIco = "./stylesheets/tree/file.png";
var DefaultExpandIco = "./stylesheets/tree/openfoldericon.png";
var DefaultUnExpandIco = "./stylesheets/tree/foldericon.png";
var DefaultExpandRightIco = "./stylesheets/tree/openfoldericon.png";
var DefaultUnExpandRightIco = "./stylesheets/tree/foldericon.png";

var DefaultExpandJoinImg = "./stylesheets/tree/Tminus.png";
var DefaultUnExpandJoinImg = "./stylesheets/tree/Tplus.png";
var DefaultExpandJoinEndImg = "./stylesheets/tree/Lminus.png";
var DefaultUnExpandJoinEndImg = "./stylesheets/tree/Lplus.png";

var DefaultJoinImg = "./stylesheets/tree/T.png";
var DefaultJoinEndImg = "./stylesheets/tree/L.png";
var DefaultJoinLineImg = "./stylesheets/tree/I.png";

/**
*tab view item class
*auth:longwuwei
*version:1.0
*date:2014-10-11
*/
function TreeViewItem(context, name,isShowCheckBox) {
    this.id = "";
    this.name = "TreeViewItem";
    this.version = "1.0.0";
    this.mContext = context;
    /**
    *item view
    */
    this.mView = null;
    this.mTitleView = null;
    this.mText = name;
    this.mIndex = -1;
    this.mLevel = 0;
    this.mIco = DefaultElementIco;
    this.mExpandIco = DefaultExpandIco;
    this.mUnExpandIco = DefaultUnExpandIco;  
    this.mExpandRightIco = DefaultExpandRightIco;
    this.mUnExpandRightIco = DefaultUnExpandRightIco;  
    this.icoView = null;
    this.checkBoxView  = null;
    this.showJoinLine = false;
    this.showCheckBox = false;  
    this.checkStatus = false;  
    this.isTailNode = false;   
    this.isMenuStyle = false; 
    /**
    *ChildTree
    */
    this.mChildTree = null;
    this.isExpandChildTree = true;
    /**
     * Parent tree or item
     */
    this.mParent = null;
    /**
    *item data
    */
    this.mUrl = null;
    this.pars = null;
    this.data = null;
}

TreeViewItem.prototype.setShowJoineLine = function (v) {
    this.showJoinLine = v;
};

TreeViewItem.prototype.setShowCheckBox = function (v) {
    this.showCheckBox = v;
};

TreeViewItem.prototype.setCheckStatus = function (v) {
    this.checkStatus = v;
};

TreeViewItem.prototype.setTailNode = function (v) {
    this.isTailNode = v;
};

TreeViewItem.prototype.setMenuStyle = function (v) {
    this.isMenuStyle = v;
};

TreeViewItem.prototype.setDataSource = function (data) {
    this.data = data;
};

TreeViewItem.prototype.getDataSource = function () {
    return this.data;
};

TreeViewItem.prototype.setLinkUrl = function (url) {
    this.mUrl = url;
};

TreeViewItem.prototype.getLinkUrl = function () {
    return this.mUrl;
};

TreeViewItem.prototype.setTag = function (tags) {
    this.pars= tags;
};

TreeViewItem.prototype.getTag = function () {
    return this.pars;
};

TreeViewItem.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};

TreeViewItem.prototype.setId = function (ids) {
    this.id = ids;
};

TreeViewItem.prototype.setIndex = function (index) {
    this.mIndex = index;
};

TreeViewItem.prototype.setLevel = function (lvl) {
    this.mLevel = lvl;
    this.updateStatus(0);
};

TreeViewItem.prototype.getLevel = function () {
    return this.mLevel;   
};

TreeViewItem.prototype.setSelected = function (e) {
    if (this.mContext != null && typeof (this.mContext.setSelected) != 'undefined') {
        this.mContext.setSelected(e);
    }
};

TreeViewItem.prototype.setExpanded = function (e,args) {
    if (this.mContext != null && typeof (this.mContext.setSelected) != 'undefined') {
        this.mContext.setExpanded(e,args);
    }
};

TreeViewItem.prototype.invalidate = function () {
    if (this.mView == null) {
        this.mView = $c("li", this.getId());
        this.mTitleView = $c("div");
        this.mView.appendChild(this.mTitleView);
        this.updateStatus(0);
        //item event
        var o = this;
        this.mTitleView.onclick = function () {
            if (o.mContext != null) {
                o.expandChildTree();
            }
        };
        this.mTitleView.onmouseover = function () {
            o.addTitleClassName("onFocus");
        };
        this.mTitleView.onmouseout = function () {
            o.rmTitleClassName("onFocus");
        };
    }
    return this.mView;
};

TreeViewItem.prototype.view = function () {
    if (this.mView != null){
        return this.mView;
    }       
    return this.invalidate();
};

TreeViewItem.prototype.updateStatus = function (flag) {
    if (this.mTitleView == null) {
        return;
    }
    this.mTitleView.innerHTML = '';    
    if(this.isMenuStyle && this.mChildTree){      
        this.mTitleView.appendChild(this.buildRightFoldIco(flag));
    }   
    this.mTitleView.appendChild(this.buildItemContent(flag));    
};

TreeViewItem.prototype.buildItemContent = function (flag) {
    var contentLayer = $c('div');
    contentLayer.style.width="100%";
    contentLayer.style.height="100%";
    this.buildItemJoinLine(contentLayer);
    //ico
    var ico = null;
    if (this.mChildTree != null) {
        if (this.isExpandChildTree) {
            ico = formatHtmlIco(this.mExpandIco, "", "", "", this.mText);
        }
        else {
            ico = formatHtmlIco(this.mUnExpandIco, "", "", "", this.mText);
        }
    }
    else if (!empty(this.mIco)) {
        ico = formatHtmlIco(this.mIco, "", "", "", this.mText);
    }         
    if (ico != null) {
        this.icoView = ico;
        contentLayer.appendChild(ico);
    }
    //checkbox
    var chkBox = createInput("checkbox", this.getId()+"_chkbox", "auto", "auto", "", "选中", "center"); 
    if (chkBox != null && this.showCheckBox) {
        var othis = this;
        chkBox.onclick = function (e) { e = e ? e : event; othis.onTreeItemCheck(this); cancelBubble(e); };
        chkBox.ondblclick = function (e) { e = e ? e : event; cancelBubble(e); };
        chkBox.index = this.mIndex;
        chkBox.tag = this.data;
        chkBox.checked = this.checkStatus;
        this.checkBoxView = chkBox;
        contentLayer.appendChild(chkBox);
    }  
    //show text
    contentLayer.appendChild(formatHtmlString(this.mText, "", "", ""));
    return contentLayer;
};

TreeViewItem.prototype.buildItemJoinLine = function (contentLayer) {
    for (var i = 0; i <= this.mLevel; i++) {
        var joinImg = DefaultBlankIco;
        if(i> 0 && this.showJoinLine){
            joinImg = DefaultJoinLineImg;
        }
        //add join img
        contentLayer.appendChild(formatHtmlIco(joinImg, "", "", "", ""));
    }
};

TreeViewItem.prototype.buildRightFoldIco = function (flag) {   
    var btnLayer = $c('div');
    btnLayer.style.width="30px";
    btnLayer.style.height="100%";
    setCssFloat(btnLayer, "right");   
    setCss(btnLayer, "rightFoldIco"); 
    if (this.isExpandChildTree) {
        btnLayer.appendChild(formatHtmlIco(this.mExpandRightIco, "", "", "", this.mText));
    }
    else {
        btnLayer.appendChild(formatHtmlIco(this.mUnExpandRightIco, "", "", "", this.mText));
    }    
    return btnLayer;
};

TreeViewItem.prototype.setExpandIco = function (icon) {
    this.mExpandIco = icon;
};

TreeViewItem.prototype.setUnExpandIco = function (icon) {
    this.mUnExpandIco = icon;
};

TreeViewItem.prototype.setExpandRightIco = function (icon) {
    this.mExpandRightIco = icon;
};

TreeViewItem.prototype.setUnExpandRightIco = function (icon) {
    this.mUnExpandRightIco = icon;
};

TreeViewItem.prototype.setIco = function (icon) {
    this.mIco = icon;
};

TreeViewItem.prototype.getName = function () {
    return this.mText;
};

TreeViewItem.prototype.setName = function (name) {
    this.mText = name;
};

TreeViewItem.prototype.setClassName = function (name) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        setCss(this.mView, name);
    }
};

TreeViewItem.prototype.setTitleClassName = function (name) {
    this.invalidate();
    if (this.mTitleView != null
        && name != null
        && typeof (name) != "undefined") {
        setCss(this.mTitleView, name);
    }
};

TreeViewItem.prototype.addTitleClassName = function (name) {
    this.invalidate();
    if (this.mTitleView != null
        && name != null
        && typeof (name) != "undefined") {
        addCss(this.mTitleView, name);
    }
};

TreeViewItem.prototype.rmTitleClassName = function (name) {
    this.invalidate();
    if (this.mTitleView != null
        && name != null
        && typeof (name) != "undefined") {
        rmCss(this.mTitleView, name);
    }
};

TreeViewItem.prototype.setChildTree = function (mtree) {
    this.invalidate();
    if (this.mView != null
        && mtree != null
        && typeof (mtree) != "undefined") {       
        mtree.setLevel(this.mLevel + 1);
        mtree.setParent(this);
        this.mChildTree = mtree;
        this.mView.appendChild(mtree.view());
        this.updateStatus(0);
    }
};

TreeViewItem.prototype.expandChildTree = function () {
    if (this.mChildTree != null) {
        if (!this.isExpandChildTree) {
            this.mChildTree.setVisible("block");
            this.isExpandChildTree = true;
            this.updateStatus(1);
        }
        else {
            this.mChildTree.setVisible("none");
            this.isExpandChildTree = false;
            this.updateStatus(1);
        }
        //setExpanded
        this.setExpanded(this,this.isExpandChildTree);
    }
    else {
        this.setSelected(this);
    }
};

TreeViewItem.prototype.unExpandChildTree = function () {
    if (this.mChildTree != null && this.isExpandChildTree) {       
        this.mChildTree.setVisible("none");
        this.isExpandChildTree = false;
        this.updateStatus(1);               
    }
};

TreeViewItem.prototype.onTreeItemCheck = function (e) {
    if (this.mChildTree != null && e != null){
        this.mChildTree.checkTreeItem(e.checked);
    }   
    if(this.mParent != null && e != null){
        //keep on parent checked
        if(e.checked){
            this.updateParentCheckStatus(true);
        }
       //notice:ParentCheckStatus
       this.noticeParentCheckStatus(e.checked); 
    }
};

TreeViewItem.prototype.checkTreeItem = function (v) {
    if(this.checkBoxView != null){
        this.checkBoxView.checked = v;
        this.checkStatus = v;
    }
    if (this.mChildTree != null){
        this.mChildTree.checkTreeItem(v);
    }
};

TreeViewItem.prototype.updateParentCheckStatus = function (v) {
    if(this.checkBoxView != null){
        this.checkBoxView.checked = v;
        this.checkStatus = v;
    }
    if(this.mParent != null){
        this.mParent.updateParentCheckStatus(v);
    }
};

TreeViewItem.prototype.noticeParentCheckStatus = function (v) {
    if(this.mParent != null){
        this.mParent.noticeParentCheckStatus(v);
    }
};

TreeViewItem.prototype.getCheckItemList = function () {
    var checkItemList = new Array();
    if(this.checkBoxView != null && this.checkBoxView.checked){
        checkItemList[0] = this.data;
    }    
    if (this.mChildTree != null){
        var childCheckItemList = this.mChildTree.getCheckItemList();
        if(childCheckItemList != null && childCheckItemList.length > 0){
            for(var i = 0;i< childCheckItemList.length;i++){
                checkItemList[checkItemList.length] = childCheckItemList[i];
            }
        }
    }
    return checkItemList;
};

TreeViewItem.prototype.getParent = function () {   
    return this.mParent;
};

TreeViewItem.prototype.setParent = function (o) {   
    this.mParent = o;
};

/**
*tree View class
*auth:lww
*version:1.0
*date:2014-10-11
*/
function TreeView(context) {
    this.id = "";
    this.name = "TreeView";
    this.version = "1.0.0";
    this.mContext = context;
    /**
    *tree info
    */
    this.mView = null;
    this.mIndex = 0;
    this.mLevel = 0;
    this.mItemList = null;   
    /**
     * tree style
     */
    this.showCheckBox = false;
    /**
    *tree current selected item
    */
    this.mCurrentSelectedItem = null;
    /**
     * parent tree or item
     */
    this.mParent = null;
}

TreeView.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};

TreeView.prototype.setId = function (ids) {
    this.id = ids;
};

TreeView.prototype.setLevel = function (lvl) {
    this.mLevel = lvl;
    if (this.mItemList != null && this.mItemList.length > 0) {
        for (var i = 0; i < this.mItemList.length; i++) {
            this.mItemList[i].setLevel(this.mLevel);
        }
    }
};

TreeView.prototype.getLevel = function () {
    return this.mLevel;
};

TreeView.prototype.setShowCheckBox = function (v) {
    this.showCheckBox = v;
};

TreeView.prototype.getLastSelectedItem = function () {
    return this.mCurrentSelectedItem;
};

//override this method
TreeView.prototype.onSelected = function (e) {
   //add your code here
};

//override this method
TreeView.prototype.onExpanded = function (e,arg) {
    //add your code here
};

//override this method
TreeView.prototype.onCheckStatusChanged = function (arg) {
    //add your code here
};

TreeView.prototype.setSelected = function (e) {
    if (this.mContext != null && typeof (this.mContext.setSelected) != 'undefined') {
        this.mContext.setSelected(e);
    }
    else { //update style                
        if (this.mCurrentSelectedItem != null) {
            this.mCurrentSelectedItem.rmTitleClassName("onSelected");
        }
        e.addTitleClassName("onSelected");
        //call user method
        this.onSelected(e);
        //save current selected event element
        this.mCurrentSelectedItem = e;
    }
};

TreeView.prototype.setExpanded = function (e,args) {
    if (this.mContext != null && typeof (this.mContext.setSelected) != 'undefined') {
        this.mContext.setExpanded(e,args);
    }
    else{ //call user method          
        this.onExpanded(e,args);
    }
};

TreeView.prototype.invalidate = function () {
    if (this.mView == null) {
        this.mView = $c("ul", this.getId());
    }
    return this.mView;
};

TreeView.prototype.view = function () {
    if (this.mView != null)
        return this.mView;
    return this.invalidate();
};

TreeView.prototype.setClassName = function (name, flag) {
    this.invalidate();
    if (this.mView != null
        && name != null
        && typeof (name) != "undefined") {
        setCss(this.mView, name);
    }
};

TreeView.prototype.addItem = function (item) {
    this.invalidate();
    /**
    *add to list
    */
    if (this.mItemList == null) {
        this.mItemList = new Array();
        this.mIndex = 0;
    }

    if (item != null) {
        item.setIndex(this.mIndex);
        item.setLevel(this.mLevel);
        item.setParent(this);
        this.mItemList[this.mIndex++] = item;
        this.mView.appendChild(item.view());
    }
};

TreeView.prototype.getSize = function () {
    if (this.mItemList == null)
        return 0;
    else
        return this.mItemList.length;
};

TreeView.prototype.getItem = function (index) {
    if (this.getSize() > 0 && index >= 0 && index < this.getSize()) {
        return this.mItemList[index];
    }
    else {
        return null;
    }
};

TreeView.prototype.setVisible = function (v) {
    if (this.mView != null) {
        this.mView.style.display = v;
    }
};

TreeView.prototype.checkTreeItem = function (v) {
    if (this.mItemList != null && this.mItemList.length > 0) {
        for(var i = 0; i < this.mItemList.length; i++){
            this.mItemList[i].checkTreeItem(v);
        }
    }
};

TreeView.prototype.updateParentCheckStatus = function (v) {
    if(this.mParent != null){
        this.mParent.updateParentCheckStatus(v);
    }   
};

TreeView.prototype.noticeParentCheckStatus = function (v) {
    if(this.mParent != null){
        this.mParent.noticeParentCheckStatus(v);
    }
    else{
        this.onCheckStatusChanged(v);
    }
};

TreeView.prototype.getCheckItemList = function () {
    var checkItemList = new Array();
    if (this.mItemList != null && this.mItemList.length > 0) {
        for(var i = 0; i < this.mItemList.length; i++){
            var childCheckItemList = this.mItemList[i].getCheckItemList();
            if(childCheckItemList != null && childCheckItemList.length > 0){
                for(var j = 0;j< childCheckItemList.length;j++){
                    checkItemList[checkItemList.length] = childCheckItemList[j];
                }
            }
        }
    }
    return checkItemList;
};

TreeView.prototype.getParent = function () {   
    return this.mParent;
};

TreeView.prototype.setParent = function (o) {   
    this.mParent = o;
};

markscript("TreeView");
//end TreeView.js